﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using Tb.App.API.Applications.Command.Slaughter;
using Tb.App.Domain.Rabbit.Slaughter;
using Tb.App.ErrorCode;

namespace Tb.App.API.Controllers.Rabbit
{
    /// <summary>
    /// 屠宰信息-控制器API
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SlaughterController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SlaughterController> logger;
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="mediator">中介者</param>
        public SlaughterController(ILogger<SlaughterController> logger,IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }
        /// <summary>
        /// 设置屠宰信息
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回添加任务</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddSlaughter(AddSlaughterModelCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 获取屠宰查询
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回查询任务</returns>
        [HttpGet]
        public async Task<ApiResult<SlaughterModel>> GetSlaughterQuery([FromQuery]GetSlaughterQueryCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 设置屠宰企业信息
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回添加任务</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddSlaughterCommpany(AddSlaughterCommpanyCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 屠宰企业信息查询
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回查询任务</returns>
        [HttpGet]
        public async Task<ApiResult<SlaughterCommpany>> GetSlaughterCommpanyQuery([FromQuery]GetSlaughterCommpanyQueryCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);
                throw;
            }
        }
    }
}
